//
//  VRFirefightingRecordFoldParentCell.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/18.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRFirefightingRecordFoldParentCell.h"

#import "VRFirefightingRecordModel.h"
#import "VRFirefightingRecordFoldChildCell.h"

@interface VRFirefightingRecordFoldParentCell() <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) UILabel *dateLbl;
@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UILabel *nameLbl;
@property (nonatomic, weak) UIImageView *accessoryImgView;
@property (nonatomic, strong) UIView *bottomLineView;

@end

@implementation VRFirefightingRecordFoldParentCell

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"VRFirefightingRecordFoldParentCell";
    
    VRFirefightingRecordFoldParentCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[VRFirefightingRecordFoldParentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.accessoryType = UITableViewCellAccessoryNone;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    self.contentView.backgroundColor = ScrollViewBgColor;
    
    [self.contentView addSubview:self.tableView];
    
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UILabel *dateLbl = [[UILabel alloc] init];
    dateLbl.textColor = MAINColor;
    dateLbl.font = VRFont(14);
    [self.contentView addSubview:dateLbl];
    self.dateLbl = dateLbl;
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.image = [UIImage imageNamed:@"fire_e"];
    iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:iconImgView];
    self.iconImgView = iconImgView;
    
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.textColor = MAINColor;
    nameLbl.font = VRFont(14);
    [self.contentView addSubview:nameLbl];
    self.nameLbl = nameLbl;
    
    UIImageView *accessoryImgView = [[UIImageView alloc] init];
    accessoryImgView.image = [UIImage imageNamed:@"fire_down"];
    accessoryImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:accessoryImgView];
    self.accessoryImgView = accessoryImgView;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60);
        make.left.right.bottom.mas_equalTo(self.contentView);
    }];
    
    [self.dateLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView.mas_top).offset(30);
    }];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(100);
        make.centerY.mas_equalTo(self.contentView.mas_top).offset(30);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [self.nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(4);
        make.centerY.mas_equalTo(self.contentView.mas_top).offset(30);
    }];
    
    [self.accessoryImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.contentView.mas_top).offset(30);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(12);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(-0);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(1);
    }];
}


#pragma mark - action



#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.dayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VRFirefightingRecordFoldChildCell *cell = [VRFirefightingRecordFoldChildCell cellWith:tableView];
    cell.model = self.model.dayData[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
    
}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsZero];
//    }
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        [cell setLayoutMargins:UIEdgeInsetsZero];
//    }
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


#pragma mark - setter && getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.01)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.sectionHeaderHeight = 0;
        _tableView.sectionFooterHeight = 0;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        //        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return  _tableView;
}

- (void)setModel:(VRFirefightingRecordMonthData *)model
{
    _model = model;
    
    self.dateLbl.text = model.month;
    self.nameLbl.text = @"本月消防存在隐患";
    self.accessoryImgView.image = model.unfold ? [UIImage imageNamed:@"fire_up"] : [UIImage imageNamed:@"fire_down"];
    self.tableView.hidden = !model.unfold;
    if (model.unfold) {
        [self.tableView reloadData];
    }
    
}

@end
